import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_hotwork/xunjian/weight/voice_player_view.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:fluttertoast/fluttertoast.dart';

///音频播放
class SoundRecordingWidget extends StatefulWidget {
  final List list;

  SoundRecordingWidget({this.list});

  @override
  _SoundRecordingWidgetState createState() => _SoundRecordingWidgetState();
}

class _SoundRecordingWidgetState extends State<SoundRecordingWidget> {
  FlutterSound flutterSound;
  int index = 0;
  StreamSubscription playerSubscription;
  var isStartPlayer = true;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Image(
                    height: 20,
                    width: 20,
                    image: AssetImage("images/shuaxin.png")),
                Text("录音文件")
              ],
            ),
          ),
          VoicePlayerView(
              widget.list[index],
              new FlutterSound(),isStartPlayer: isStartPlayer,title: "第${index+1}首",duration: "00:00"),
          MaterialButton(
            child: Text("下一首"),
            onPressed: (){
              if(index == widget.list.length){
                Fluttertoast.showToast(msg: "已经是最后一首了！");
                return;
              }
              if(playerSubscription != null){
                playerSubscription.cancel();
              }
              index ++;
              isStartPlayer = true;
              setState(() {});
            },
          ),
          MaterialButton(
            child: Text("上一首"),
            onPressed: (){
              if(index == 0){
                Fluttertoast.showToast(msg: "已经是第一首了！");
                return;
              }
              if(playerSubscription != null){
                playerSubscription.cancel();
              }
              index --;
              isStartPlayer = true;
              setState(() {});
            },
          ),
        ],
      ),
    );
  }
}
